﻿#region Copyright Notice
// This file is part of GestionPedago.
// <description>
// Copyright (C) 2012 ENI Ecole Informatique
// 
// GestionPedago is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// <program> is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using GestionPedago.EntityContext;
using GestionPedago.Services.Framework;
using GestionPedago.Services.ES.Modele;
using GestionPedago.Persistence.OPManager;

namespace GestionPedago.Services.Mapping
{
    /// <summary>
    /// Classe de mapping Entity<->ES de l'objet Activite
    /// </summary>
    public class ActiviteMappeur : IMappeur<Activite, ActiviteES>
    {
        /// <summary>
        /// Constructeur
        /// </summary>
        public ActiviteMappeur() { }

        /// <summary>
        /// Mapping Entity -> ES
        /// </summary>
        /// <param name="entity">L'entity à mapper</param>
        /// <returns>L'ES obtenu</returns>
        public ActiviteES Entity2ES(Activite entity)
        {
            ActiviteES retour = new ActiviteES();
            retour.IdActivite = entity.IdActivite;
            retour.Libelle = entity.Libelle;
            //TODO implémenter mapping Entity<->ES manuellement ! : Titre
            retour.ListeCompetenceParActivite = new List<CompetenceParActiviteES>();
            foreach (CompetenceParActivite cpa in entity.CompetenceParActivite)
            {
                retour.ListeCompetence.Add(new CompetenceMappeur().Entity2ES(cpa.GetCompetence()));
                retour.ListeCompetenceParActivite.Add(new CompetenceParActiviteMappeur().Entity2ES(cpa));
            }
            return retour;
        }

        /// <summary>
        /// Mapping ES -> Entity
        /// </summary>
        /// <param name="es">L'ES à mapper</param>
        /// <returns>L'entity obtenu</returns>
        public Activite ES2Entity(ActiviteES es)
        {
            Activite retour = new Activite();
            retour.IdActivite = es.IdActivite;
            bool alreadyAttached = !EntityStateChecker.IsDetached(retour);
            if (!alreadyAttached)
            {
                EntityConnection.GetContext().Activite.Attach(retour);
            }
            retour.Libelle = es.Libelle;
            //TODO implémenter mapping Entity<->ES manuellement ! : Titre
            foreach (CompetenceParActiviteES cpae in es.ListeCompetenceParActivite)
            {
                retour.CompetenceParActivite.Add(new CompetenceParActiviteMappeur().ES2Entity(cpae));
            }
            foreach (CompetenceES ee in es.ListeCompetence)
            {

                CompetenceParActivite cpa = new CompetenceParActivite();
                cpa.IdCompetence = ee.IdCompetence;
                cpa.IdActivite = retour.IdActivite;
                if (!retour.ContientCompetenceParActivite(cpa))
                {
                    cpa = CompetenceParActiviteOPManager.AjouterCompetenceParActivite(cpa);
                    es.ListeCompetenceParActivite.Add(new CompetenceParActiviteMappeur().Entity2ES(cpa));
                }

            }

            List<CompetenceParActiviteES> lstCompetencesARetirer = new List<CompetenceParActiviteES>();
            foreach (CompetenceParActiviteES cpae in es.ListeCompetenceParActivite)
            {
                CompetenceES ce = new CompetenceES();
                ce.IdCompetence = cpae.IdCompetence;
                if (!es.ListeCompetence.Contains(ce))
                {
                    lstCompetencesARetirer.Add(cpae);
                }
            }

            foreach (CompetenceParActiviteES cpae in lstCompetencesARetirer)
            {
                es.ListeCompetenceParActivite.Remove(cpae);
                CompetenceParActiviteOPManager.SupprimerCompetenceParActivite(CompetenceParActiviteOPManager.GetCompetenceParActiviteByIdCompetenceIdActivite(cpae.IdCompetence, cpae.IdActivite));
            }


            if (alreadyAttached)
            {
                EntityConnection.GetContext().Activite.ApplyCurrentValues(retour);
            }
            return retour;
        }
    }
}
